﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for LogManager
/// </summary>
public class LogManager
{
    private static string logPath = string.Empty;
    static object locker = new object();

    /// <summary>
    /// 保存日志的文件夹
    /// </summary>
    public static string LogPath
    {
        get
        {
            if (logPath == string.Empty)
            {
                if (System.Web.HttpContext.Current == null)
                {
                    // Windows Forms 应用
                    logPath = AppDomain.CurrentDomain.BaseDirectory;
                }
                else
                {
                    // Web 应用
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"log\";
                }
            }
            return logPath;
        }
        set { logPath = value; }
    }

    private static string logFilePrefix = "Midea HFM ";
    /// <summary>
    /// 日志文件前缀
    /// </summary>
    public static string LogFilePrefix
    {
        get { return logFilePrefix; }
        set { logFilePrefix = value; }
    }

    /// <summary>
    /// 写日志
    /// </summary>
    public static void WriteLog(string logFile, string msg)
    {
        try
        {
            String strContent = "";
            if (System.Web.HttpContext.Current.Request.UserHostAddress.Equals(System.Web.HttpContext.Current.Request.UserHostName))
            {
                strContent = String.Format("[{0}] [{1}] {2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), System.Web.HttpContext.Current.Request.UserHostAddress, msg.Replace("\r", "").Replace("\n", " "));
            }
            else
            {
                strContent = String.Format("[{0}] [{1} {2}] {3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), System.Web.HttpContext.Current.Request.UserHostAddress, System.Web.HttpContext.Current.Request.UserHostName, msg.Replace("\r", "").Replace("\n", " "));
            }

            lock (locker)
            {
                /*
                String FolderPath = LogPath +"\\" ;

                if (!System.IO.Directory.Exists(FolderPath))
                {
                    System.IO.Directory.CreateDirectory(FolderPath);
                }
                */

                String FullLogPath = LogPath + LogFilePrefix + logFile + " " + DateTime.Now.ToString("yyyyMM") + ".Log";

                /*

                System.IO.Directory.CreateDirectory(LogPath + LogFilePrefix + logFile + " " +
                    DateTime.Now.ToString("yyyyMM"));
                */

                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    FullLogPath
                    );

                sw.WriteLine(strContent);
                sw.Close();
            }
        }
        catch
        {
            throw;
        }
    }

    /// <summary>
    /// 写日志
    /// </summary>
    public static void WriteLog(LogFile logFile, string msg)
    {
        WriteLog(logFile.ToString(), msg);
    }

    public static void WriteLog(string msg)
    {
        WriteLog ("", msg);
    }
}

/// <summary>
/// 日志类型
/// </summary>
public enum LogFile
{
    Trace,
    Warning,
    Error,
    SQL
}
